package com.ts.hidden.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Binder;
import android.os.IBinder;
import android.view.Surface;

import com.ts.hidden.dto.DataDbDto;
import com.ts.tube.utils.FilterLog;

public class ServiceMedia extends Service {
    private static final String TAG = "ServiceMedia";
    FilterLog log = new FilterLog(TAG);
    private MediaPlayer player;
    private LocalBinder mBinder = new LocalBinder();
    private String source;
    private List<DataDbDto> list = new ArrayList<DataDbDto>();

    public static Intent getIntentService(Context context) {
        return new Intent(context, ServiceMedia.class);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        log.v("log>>>" + "ServiceMedia onCreate");
        initController();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        log.e("log>>>" + "ServiceMedia onDestroy");
    }

    public class LocalBinder extends Binder {
        public ServiceMedia getService() {
            return ServiceMedia.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private void initController() {
        player = new MediaPlayer();
        player.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                playNextVideo();
                playVideo();
            }
        });

        player.setOnErrorListener(new OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                playNextVideo();
                playVideo();
                return false;
            }
        });
    }

    private void playNextVideo() {

    }

    public void playVideo() {
        player.start();
    }

    public void setSource(String source) {
        this.source = source;
    }

    public void setListData(List<DataDbDto> list) {
        this.list.clear();
        this.list.addAll(list);
    }
    
    public MediaPlayer getPlayer() {
        return player;
    }

}
